package momo.multitree.algo;

import momo.multitree.structure.*;

public class KrzyAlgo
{
	private Graph g;
	
	private CostLastTree bestOptimizer;
	
	
	public KrzyAlgo(Graph g)
	{
		this.g = g;
	}
	
	public Tree optimumTree()
	{
		Node[] nodes = g.getNodes().toArray(new Node[0]) ;
		if ( nodes.length == 0 )
			return null;
		
		CostLastTree optimizer = new CostLastTree(g, nodes[0]);
		Tree initTree = optimizer.makeLast();
		bestOptimizer = optimizer;
		
		for(int i=1; i<nodes.length; i++)
		{
			optimizer = new CostLastTree(g, nodes[i]);
			Tree nextTree = optimizer.makeLast();
			if ( initTree.compCost(false) > nextTree.compCost(false) ) {
				initTree = nextTree;
				bestOptimizer = optimizer;
			}
		}
		return initTree;
	}
	
	public CostLastTree getOptimizer() {
		return bestOptimizer;
	}
}
